using System;
using System.Data;
using Company.BLL.WS.GC;
using System.Data.SqlClient;
using Company.BLL.Utils;

namespace Company.BLL.GC
{
    public class HopDong : HopDongBase
    {
        private NguyenPhuLieuCollection _NPLCollection = new NguyenPhuLieuCollection();
        private SanPhamCollection _SPCollection = new SanPhamCollection();
        private ThietBiCollection _TBCollection = new ThietBiCollection();
        private PhuKienCollection _PKCollection = new PhuKienCollection();
        private NhomSanPhamCollection _NhomSanPham = new NhomSanPhamCollection();
        public NguyenPhuLieuCollection NPLCollection
        {
            get { return this._NPLCollection; }
            set { this._NPLCollection = value; }
        }

        public SanPhamCollection SPCollection
        {
            get { return this._SPCollection; }
            set { this._SPCollection = value; }
        }

        public ThietBiCollection TBCollection
        {
            get { return this._TBCollection; }
            set { this._TBCollection = value; }
        }

        public PhuKienCollection PKCollection
        {
            get { return this._PKCollection; }
            set { this._PKCollection = value; }
        }

        public NhomSanPhamCollection NhomSPCollection
        {
            set { _NhomSanPham = value; }
            get { return _NhomSanPham; }
        }
        public static bool UpdateRegistedToDatabase(string maHaiQuan, string maDoanhNghiep,SqlTransaction transaction)
        {
            DataSet ds = WS_GetDanhSachDaDangKy(maHaiQuan, maDoanhNghiep);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                HopDong hd = new HopDong();
                hd.MaHaiQuan = maHaiQuan;
                hd.MaDoanhNghiep = maDoanhNghiep;
                hd.SoHopDong = row["SoHopDong"].ToString();
                hd.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                hd.NgayHetHan = Convert.ToDateTime(row["NgayHetHan"]);
                if (row["NgayGiaHan"] != DBNull.Value)
                    hd.NgayGiaHan = Convert.ToDateTime(row["NgayGiaHan"]);
                hd.NuocThue_ID = row["NuocThue_ID"].ToString();
                hd.NguyenTe_ID = row["NguyenTe_ID"].ToString();
                hd.TrangThaiXuLy = 1;
                hd.InsertUpdateTransaction(transaction);
            }
            return true;
        }

        public static DataSet WS_GetDanhSachDaDangKy(string maHaiQuan, string maDoanhNghiep)
        {
            GCService service = new GCService();
            return service.HopDong_GetDanhSach(maHaiQuan, maDoanhNghiep);
        }

        public void updateDatabase(string maHaiQuan, string maDoanhNghiep)
        {
           
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    HopDong.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep, transaction);
                    NhomSanPham.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep, transaction);
                    NguyenPhuLieu.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep, transaction);
                    SanPham.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep, transaction);
                    ThietBi.UpdateRegistedToDatabase(maHaiQuan, maDoanhNghiep, transaction);
                    transaction.Commit();                                           
                        
                }
                catch(Exception ex)
                {                   
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
            
        }
        public void DongBoHaiQuan()
        {
            GCService service = new GCService();
            DataSet ds= service.HopDong_GetDanhSachOfHopDong(this.MaHaiQuan, this.MaDoanhNghiep,this.SoHopDong,this.NgayKy);
            DataSet dsLoaiSanPham = service.LoaiSanPham_GetDanhSachOfHopDong(this.MaHaiQuan, this.MaDoanhNghiep, this.SoHopDong, this.NgayKy);
            DataSet dsSanPham = service.SanPham_GetDanhSachOfHopDong(this.MaHaiQuan, this.MaDoanhNghiep, this.SoHopDong, this.NgayKy);
            DataSet dsNguyenPhuLieu = service.NguyenPhuLieu_GetDanhSachOfHopDong(this.MaHaiQuan, this.MaDoanhNghiep, this.SoHopDong, this.NgayKy);
            DataSet dsThietBi = service.ThietBi_GetDanhSachOfHopDong(this.MaHaiQuan, this.MaDoanhNghiep, this.SoHopDong, this.NgayKy);
                  
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    //hop dong                    
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        HopDong hd = new HopDong();
                        hd.MaHaiQuan = this.MaHaiQuan;
                        hd.MaDoanhNghiep = this.MaDoanhNghiep;
                        hd.SoHopDong = row["SoHopDong"].ToString();
                        hd.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        hd.NgayHetHan = Convert.ToDateTime(row["NgayHetHan"]);
                        if (row["NgayGiaHan"] != DBNull.Value)
                            hd.NgayGiaHan = Convert.ToDateTime(row["NgayGiaHan"]);
                        hd.NuocThue_ID = row["NuocThue_ID"].ToString();
                        hd.NguyenTe_ID = row["NguyenTe_ID"].ToString();
                        hd.TrangThaiXuLy = 1;
                        hd.InsertUpdateTransaction(transaction);
                    }                  
                    foreach (DataRow row in dsLoaiSanPham.Tables[0].Rows)
                    {
                        NhomSanPham nhomsp = new NhomSanPham();
                        nhomsp.SoHopDong = row["SoHopDong"].ToString();
                        nhomsp.MaHaiQuan = row["MaHaiQuan"].ToString();
                        nhomsp.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        nhomsp.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        nhomsp.Ma = row["Ma"].ToString().Substring(1);
                        nhomsp.Ten = Company.BLL.DuLieuChuan.NhomSanPham.getTenSanPham(nhomsp.Ma);
                        nhomsp.SoLuongDangKy = Convert.ToDecimal(row["SoLuong"]);
                        nhomsp.TriGia = Convert.ToDecimal(row["Gia"]);
                        nhomsp.InsertUpdateTransaction(transaction);
                    }
                    foreach (DataRow row in dsSanPham.Tables[0].Rows)
                    {
                        SanPham sp = new SanPham();
                        sp.SoHopDong = row["SoHopDong"].ToString();
                        sp.MaHaiQuan = row["MaHaiQuan"].ToString();
                        sp.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        sp.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        sp.Ma = row["Ma"].ToString().Substring(1);
                        sp.Ten = FontConverter.TCVN2Unicode(row["Ten"].ToString());
                        sp.MaHS = row["MaHS"].ToString();
                        sp.DVT_ID = row["DVT_ID"].ToString();
                        sp.SoLuongDangKy = Convert.ToDecimal(row["SoLuongDangKy"]);
                        sp.InsertUpdateTransaction(transaction);
                    }
                    foreach (DataRow row in dsNguyenPhuLieu.Tables[0].Rows)
                    {
                        NguyenPhuLieu npl = new NguyenPhuLieu();
                        npl.SoHopDong = row["SoHopDong"].ToString();
                        npl.MaHaiQuan = row["MaHaiQuan"].ToString();
                        npl.MaDoanhNghiep = row["MaDoanhNghiep"].ToString();
                        npl.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        npl.Ma = row["Ma"].ToString().Substring(1);
                        npl.Ten = FontConverter.TCVN2Unicode(row["Ten"].ToString());
                        npl.MaHS = row["MaHS"].ToString();
                        npl.DVT_ID = row["DVT_ID"].ToString();
                        npl.SoLuongDangKy = Convert.ToDecimal(row["SoLuongDangKy"]);
                        npl.InsertUpdateTransaction(transaction);
                    }
                    foreach (DataRow row in dsThietBi.Tables[0].Rows)
                    {
                        ThietBi tb = new ThietBi();
                        tb.SoHopDong = row["SoHopDong"].ToString();
                        tb.MaHaiQuan = this.MaHaiQuan;
                        tb.MaDoanhNghiep = this.MaDoanhNghiep;
                        tb.NgayKy = Convert.ToDateTime(row["NgayKy"]);
                        tb.Ma = row["Ma"].ToString().Substring(1);
                        tb.Ten = FontConverter.TCVN2Unicode(row["Ten"].ToString());
                        tb.MaHS = row["MaHS"].ToString();
                        tb.DVT_ID = row["DVT_ID"].ToString();
                        tb.SoLuongDangKy = Convert.ToDecimal(row["SoLuongDangKy"]);
                        tb.NuocXX_ID = row["NuocXX_ID"].ToString();
                        tb.TinhTrang = row["TinhTrang"].ToString();
                        tb.DonGia = Convert.ToDecimal(row["DonGia"].ToString());
                        tb.TriGia = Convert.ToDecimal(row["TriGia"].ToString());
                        tb.NguyenTe_ID = row["NguyenTe_ID"].ToString();
                        tb.InsertUpdateTransaction(transaction);
                    }
                    
                    transaction.Commit();

                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        public bool GetHopDongOnHaiQuan(long index)
        {
            GCService service = new GCService();
            DataSet ds = service.HopDong_GetHopDong(this.MaHaiQuan, this.MaDoanhNghiep,index);
            if (ds == null)
                return false;
            DataRow row = ds.Tables[0].Rows[0];
            this.SoHopDong = row["SoHopDong"].ToString();
            this.NgayKy=Convert.ToDateTime(row["NgayKy"].ToString());
            this.DongBoHaiQuan();
            return true;
        }
    }
}